package com.atguigu.web;

import com.atguigu.pojo.User;
import com.atguigu.service.UserService;
import com.atguigu.service.impl.UserServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "LoginServlet", value = "/LoginServlet")
public class LoginServlet extends HttpServlet {
   private UserService userService = new UserServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
        String password = req.getParameter("password");
       User user = userService.login(new User(null,username,password,null));
       if (user == null){
//          把错误信息，和回显表单信息，保存到Request域中
           req.setAttribute("msg","用户名或密码错误");
           req.setAttribute("username", username);

           System.out.println("用户名或密码错误");
//           跳回登陆页面
           req.getRequestDispatcher("/pages/user/login.jsp").forward(req,resp);
       }else {
           System.out.println("登陆成功");
           req.getRequestDispatcher("/pages/user/login_success.jsp").forward(req,resp);
       }
    }
}
